﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.OtherForms
{
    public partial class ThemPhieuThanhToan : Form
    {
        private string _maPhieu;

        public ThemPhieuThanhToan()
        {
            InitializeComponent();
        }

        public ThemPhieuThanhToan(string maPhieuThanhToan)
        {
            InitializeComponent();
            _maPhieu = maPhieuThanhToan;
        }

        private void ThemPhieuThanhToan_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();

            // fill combobox
            var listQuyTrinh = from qt in dc.QuyTrinhMuaHangs
                               where qt.deleted == false &&
                                     qt.TrangThai == _DB.TrangThaiQuyTrinhMuaHang.DA_NHAP_KHO
                               select qt;

            cbbPhieuMuaHang.Items.Clear();
            cbbPhieuTamUng.Items.Clear();
            foreach (_DB.QuyTrinhMuaHang qt in listQuyTrinh)
            {
                cbbPhieuMuaHang.Items.Add(qt.PhieuMuaHang);
                if (qt.PhieuTamUng != null)
                    cbbPhieuTamUng.Items.Add(qt.PhieuTamUng);
            }

            // if edit, fill info
            if (_maPhieu != null)
            {
                _DB.PhieuThanhToan p = dc.PhieuThanhToans.Single(x => x.MaPhieu == _maPhieu);
                _DB.QuyTrinhMuaHang qt = p.QuyTrinhMuaHangs.Single();
                txtMaPhieu.Text = p.MaPhieu;
                txtMaPhieu.ReadOnly = true;
                txtNoiDung.Text = p.NoiDung;
                cbbPhieuMuaHang.Items.Add(qt.PhieuMuaHang);
                cbbPhieuMuaHang.SelectedItem = p.QuyTrinhMuaHangs.Single().PhieuMuaHang;
                if (qt.PhieuTamUng != null)
                {
                    cbbPhieuTamUng.Items.Add(qt.PhieuTamUng);
                    cbbPhieuTamUng.SelectedItem = qt.PhieuTamUng;
                }
                
                cbbPhieuMuaHang.Enabled = false;
                cbbPhieuTamUng.Enabled = false;
                updateTien();
            }
            else
                txtMaPhieu.Text = _DB.DinhDangMaPhieu.getMaPhieuMoi(_DB.DinhDangMaPhieu.DINH_DANG_PHIEU_THANH_TOAN_ID);
        }


        private void updateTien()
        {
            _DB.PhieuMuaHang pmh = (_DB.PhieuMuaHang)cbbPhieuMuaHang.SelectedItem;
            _DB.PhieuTamUng ptu = (_DB.PhieuTamUng)cbbPhieuTamUng.SelectedItem;

            float tienThanhToan = 0;

            if (pmh != null)
            {
                tienThanhToan += pmh.getTongTien();
                txtTongTien.Text = pmh.getTongTien().ToString("#,#.##");
            }
            else
                txtTongTien.Text = "0";

            if (ptu != null)
            {
                tienThanhToan -= ptu.SoTien;
                txtTienTamUng.Text = ptu.SoTien.ToString("#,#.##");
            }
            else
                txtTienTamUng.Text = "0";

            if (tienThanhToan > 0)
                txtTienThanhToan.Text = "Phải trả  " + tienThanhToan.ToString("#,#.##");
            else
                txtTienThanhToan.Text = "Phải thu  " + (-tienThanhToan).ToString("#,#.##");

 
        }

        private void cbbPhieuMuaHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            _DB.PhieuMuaHang p = (_DB.PhieuMuaHang)cbbPhieuMuaHang.SelectedItem;

            if (p != null)
            {
                if (cbbPhieuTamUng.SelectedItem != p.QuyTrinhMuaHangs.Single().PhieuTamUng)
                    cbbPhieuTamUng.SelectedItem = p.QuyTrinhMuaHangs.Single().PhieuTamUng;
                //txtPhieuNhapKho.Text = p.QuyTrinhMuaHangs.Single().MaPhieuNhapKho;
            }

            updateTien();
        }

        private void cbbPhieuTamUng_SelectedIndexChanged(object sender, EventArgs e)
        {
            _DB.PhieuTamUng p = (_DB.PhieuTamUng)cbbPhieuTamUng.SelectedItem;
            if (p != null)
                cbbPhieuMuaHang.SelectedItem = p.QuyTrinhMuaHangs.Single().PhieuMuaHang;
        }

        /////////////////////////////////////////
        //  OK, Cancel
        ////////////////////////////////////////

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                // edit case
                _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
                _DB.PhieuThanhToan p;
                if (_maPhieu != null)
                {
                    p = dc.PhieuThanhToans.Single(x => x.MaPhieu == _maPhieu);
                    p.NoiDung = txtNoiDung.Text;
                    dc.SubmitChanges();
                    this.Close();
                    return;
                }

                // new case
                _DB.PhieuMuaHang pmh = (_DB.PhieuMuaHang) cbbPhieuMuaHang.SelectedItem;
                p = new _DB.PhieuThanhToan();
                p.MaPhieu = txtMaPhieu.Text;
                p.MaNguoiTao = Program.MaNguoiDungHienTai;
                p.NgayTao = DateTime.Now;
                p.NoiDung = txtNoiDung.Text;
                
                // save
                dc.PhieuThanhToans.InsertOnSubmit(p);
                _DB.QuyTrinhMuaHang qt = dc.QuyTrinhMuaHangs.Single(x => x.MaPhieuMuaHang == pmh.MaPhieu);
                qt.MaPhieuThanhToan = p.MaPhieu;
                qt.TrangThai = _DB.TrangThaiQuyTrinhMuaHang.CHINH_SUA_PHIEU_THANH_TOAN;
                dc.SubmitChanges();
                this.Close();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /////////////////////////////////////////
        // Xem
        ////////////////////////////////////////

        private void button3_Click(object sender, EventArgs e)
        {
            _DB.PhieuMuaHang p = (_DB.PhieuMuaHang)cbbPhieuMuaHang.SelectedItem;
            if (p != null)
            {
                OtherForms.XemPhieuMuaHang f = new OtherForms.XemPhieuMuaHang(p.MaPhieu);
                f.ShowDialog();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            _DB.PhieuTamUng p = (_DB.PhieuTamUng)cbbPhieuTamUng.SelectedItem;
            if (p != null)
            {
                OtherForms.XemPhieuTamUng f = new OtherForms.XemPhieuTamUng(p.MaPhieu);
                f.ShowDialog();
            }
        }
    }
}
